﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Orange.Mvc4App.Models;

namespace Orange.Mvc4App.Services {

    public interface IProductService : IBaseService<Product> {
        Product GetNext();
    }

    public class ProductService : BaseService, IProductService {
        public IList<Product> GetList(Func<Product, bool> filters) {
            if (filters != null)
                return db.Products.Where(filters).ToList();
            else
                return db.Products.ToList();
        }

        public Product Find(params object[] keys) {
            return db.Products.Find(keys);
        }

        public void Add(Product model) {
            db.Products.Add(model);
        }

        public void Update(Product model) {
            db.Entry(model).State = EntityState.Modified;
        }

        public void Delete(Product model) {
            db.Products.Remove(model);
        }

        public void Save() {
            db.SaveChanges();
        }

        public void Dispose() {
            db.Dispose();
        }

        public Product GetNext() {
            int id = 1;
            if (db.Products.Count() > 0)
                id = db.Products.Max(c => c.Id) + 1;
            return new Product { Id = id, Status = "A", };
        }
    }
}